package com.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.ProjectFee;
import com.project.entity.ProjectManagement;
import com.project.service.ChargeManage;
import com.project.entity.*;

public class ChargeManageAction {
	private String 	start,limit;
	private String	jsonString;
	private String 	numberQuery;
	private String 	nameQuery;
	
	private ChargeManage	chargeManageService;
	private ProjectFee		projectFee;
	
	
	
	public String getNameQuery() {
		return nameQuery;
	}
	public void setNameQuery(String nameQuery) {
		this.nameQuery = nameQuery;
	}
	public String getNumberQuery() {
		return numberQuery;
	}
	public void setNumberQuery(String numberQuery) {
		this.numberQuery = numberQuery;
	}
	public ProjectFee getProjectFee() {
		return projectFee;
	}
	public void setProjectFee(ProjectFee projectFee) {
		this.projectFee = projectFee;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public ChargeManage getChargeManageService() {
		return chargeManageService;
	}
	public void setChargeManageService(ChargeManage chargeManageService) {
		this.chargeManageService = chargeManageService;
	}
	
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	
	void clearParams(){
		this.start = null;
		this.limit = null;
		this.nameQuery = null;
		this.numberQuery = null;
	}
	
	
	/**
	 * 查看收费管理单
	 * @return
	 */
	public String getAllProjectFee(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")==null){
			this.setJsonString("{success:false,totalCount : " + 0
					+ ", list:}");
			return "success";
		}
		
		Map<String,String> map = new HashMap<String,String>();
		if(context.getSession().get("right") == "manager"){
			int id = (Integer)context.getSession().get("id");
			map.put("followupperson", String.valueOf(id));
		}
			
		map.put("start", start);
		map.put("limit", limit);
		if(this.numberQuery != null && !this.numberQuery.equals("")){
			//根据number查询
			map.put("numberQuery", numberQuery);
		}
		if(this.nameQuery != null && !this.nameQuery.equals("")){
			//根据number查询
			map.put("nameQuery", nameQuery);
		}
		List<ProjectFee>	list = this.chargeManageService.getAllProjectFee(map);
		
		int size;
		if(this.nameQuery != null &&!this.nameQuery.equals("")
				|| this.numberQuery != null && !this.numberQuery.equals("")){
			size = list.size();
		}else
			size = this.chargeManageService.countProjectFee(map);
		
		JSONArray jsonObject = JSONArray.fromObject(list);
		this.setJsonString("{success:true,totalCount : " + size
				+ ", list:" + jsonObject.toString() + "}");
		clearParams();
		return "success";
		
	}
	
	/**
	 * 新增projectFee
	 * @return
	 */
	public String addProjectFee(){
		int i;
		if(this.projectFee.getProjectnumber()!=null && !this.projectFee.getProjectnumber().equals("")){
			if ( this.projectFee.getProjectservicefeeid()!= null 
					&& !this.projectFee.getProjectservicefeeid().equals("")){
				i = this.chargeManageService.updateProjectFee(projectFee);
			} else {
				i = this.chargeManageService.insertProjectFee(projectFee);
			}
			this.setJsonString("{success:true,totalCount : " +1
					+ ", list:" + ""+ "}");
		} else{
			this.setJsonString("{failure:true,totalCount : " +0
					+ ", list:" + ""+ "}");
		}
		this.projectFee = null;
		clearParams();
		return "success";
	}
	
	public String getAllPerson(){
		List<PersonInfo> list;
		try{
			Map<String,String> map = new HashMap<String,String>();
			map.put("start", start);
			map.put("limit", limit);
			list = this.chargeManageService.getAllPerson(map);
			JSONArray jsonObject = JSONArray.fromObject(list);
			this.setJsonString("{success:true,totalCount : " +this.chargeManageService.getPersonCount()
					+ ", list:" + jsonObject.toString()+ "}");
		}catch(Exception e){
			this.setJsonString("{success:false,totalCount : " +0
					+ ", list:" + ""+ "}");
		}

		clearParams();
		return "success";
	}
	
	public String getAllUnit(){
		List<UnitInfo> list;
		try{
			Map<String,String> map = new HashMap<String,String>();
			map.put("start", start);
			map.put("limit", limit);
			list = this.chargeManageService.getAllUnit(map);
			JSONArray jsonObject = JSONArray.fromObject(list);
			this.setJsonString("{success:true,totalCount : " +this.chargeManageService.getUnitCount()
					+ ", list:" + jsonObject.toString()+ "}");
		}catch(Exception e){
			this.setJsonString("{success:false,totalCount : " +0
					+ ", list:" + ""+ "}");
		}
		clearParams();
		return "success";
	}
	
	/**
	 * 
	 * @return
	 */
	public String updateProjectFee(){
		if(this.projectFee != null){
			int i ;
			i= this.chargeManageService.updateProjectFee(projectFee);
			if(i == -1)
				this.setJsonString("{success:true,totalCount : " +-1
						+ ", list:" +""+ "}");
			else
				this.setJsonString("{success:true,totalCount : " +i
						+ ", list:" + ""+ "}");
			
		}else
			this.setJsonString("{success:true,totalCount : " +-2
					+ ", list:" + ""+ "}");
		return "success";
	}
}
